package com.hc.weather.controller;

import com.hc.weather.domain.City;
import com.hc.weather.domain.WeatherResponse;
import com.hc.weather.service.CityService;
import com.hc.weather.service.WeatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*@Controller   //类中的接口方法既能返回文本给浏览器窗口  又能跳转页面
@RestController //两个作用：1.标明当前类是一个能够处理用请求的控制器，2.能够把接口返回的对象序列化成字符串给到浏览器显示出来
@ResponseBody*/
public class WeatherController {

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    CityService cityService;
    @Autowired
    WeatherService weatherService;

//    定义接口方法
//    方法名字:weather
//    方法参数:city
//    功能:提供给用户使用,用户在浏览器地址输入:     http://localhost:8080/weather?city=110100
//  为了尝试码云仓库提交111

    //123

    @RequestMapping(value = "/weather")   //springMVC:参数绑定:http://localhost:8080/weather?city=130700
    public String weather(String city){
//        如何调用第三方接口?  需要使用一个类 RestTemplate  AmqpTemplate
        /*
        * 动物园
        * get请求:/zoo/id
        * post请求: /zoo   id,naem,loc,phon
        * put请求: /zoo   id,naem,loc,phon
        * delete请求:/zoo/id
        *
        * */

//       String result =  restTemplate.getForObject("https://apis.tianapi.com/tianqi/index?key=2af8ed08605c20f76f8183d1b702474b&city="+city+"&type=7",String.class);
        String result=restTemplate.getForObject("https://apis.tianapi.com/tianqi/index?key=2af8ed08605c20f76f8183d1b702474b&city="+city+"&type=7",String.class);
        //获取城市列表数据
        List<City> cityList = cityService.cityList();

        for(City c:cityList){
            System.out.println("city:"+c.getCityid()+","+c.getCity());
        }

       return result;


    }

    @RequestMapping(value = "/weather_v2")  //springMVC:参数绑定  http://localhost:8080/weatehr_v2?city=130700
    public WeatherResponse weather_v2(String city,Model model){

        // String result=restTemplate.getForObject("https://apis.tianapi.com/tianqi/index?key=2af8ed08605c20f76f8183d1b702474b&city="+city+"&type=7",String.class);

        //先查询缓存没有再去访问第三方接口
        WeatherResponse weatherResponse=weatherService.queryWeatherForUser(city);

        List<City> cityList=cityService.cityList();

        for(City c:cityList){
            System.out.println("city:"+c.getCityid()+","+c.getCity());
        }


        return weatherResponse;

    }

    //springMVC:参数绑定  http://localhost:8080/weatehr_v3?city=130700
    @RequestMapping(value = "/weather_v3")   //请求视图路径 不能与请求页面路径一致

    public ModelAndView weather_v3(String city,Model model){

        //先查询缓存没有再去访问第三方接口
        WeatherResponse weatherResponse=weatherService.queryWeatherForUser(city);

        List<City> cityList=cityService.cityList();

//        把数据传给页面
        model.addAttribute("cityList",cityList);
        model.addAttribute("weatherData",weatherResponse);

        return new ModelAndView("weather");  //访问输出的页面

    }

}
